package edu.gatech.ece6102;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * User: deepal
 * Date: Oct 5, 2010
 * Time: 1:23:29 PM
 */
public class TcpTunnel {
    public static void main(String args[]) throws IOException {
//        if (args.length != 3 && args.length != 4) {
//            System.err.println("Usage: java TcpTunnel listenport tunnelhost tunnelport [encoding]");
//            System.exit(1);
//        }
           int listenport = 8000;
           String tunnelhost ="localhost";
           int tunnelport = 8080;
           String enc;

               enc = "8859_1";
           System.out.println("TcpTunnel: ready to rock and roll on port " + listenport);
           ServerSocket ss = new ServerSocket(listenport);
           while (true) {
               // accept the connection from my client
               Socket sc = ss.accept();

               // connect to the thing I'm tunnelling for
               Socket st = new Socket(tunnelhost, tunnelport);
               System.out.println("TcpTunnel: tunnelling port " + listenport + " to port " + tunnelport + " on host " + tunnelhost);

               // relay the stuff thru
               new Relay(sc.getInputStream(), st.getOutputStream(), System.out, enc, false).readClient();
               new Relay(st.getInputStream(), sc.getOutputStream(), System.out, enc, true).start();
               // that's it .. they're off; now I go back to my stuff.
           }
       }
   }

